﻿module Sustainalytics.GCCS {

    import Common = Sustainalytics.Common;

    export class GCOverallCompanySummaryExtendedService implements IGCOverallCompanySummaryExtendedService {

        constructor(
            private $q: ng.IQService,
            private $http: ng.IHttpService,
            private notificationFactory: Common.INotificationFactory,
            private staticObjects: Common.IStaticObjects) {
        }

        // GET
        public Get(listOptions: Common.ListOptions, overallAssessment?: GCAssessment): ng.IPromise<any> {
            var args = $.param(
                {
                    listOptions: listOptions,
                    overallAssessment: overallAssessment
                });

            var def = this.$q.defer();

            var httpPromise: ng.IHttpPromise<any> = this.$http.get('/gccsserv/api/v1/gcoverallcompanysummaryextended?' + args);

            httpPromise
                .success(function (data) {
                    def.resolve(data);
                })
                .error(function (data, status, headers, config) {
                    def.reject({ data: data, status: status, headers: headers, config: config });
                });

            return def.promise;
        }

        // get items count
        public GetCount(overallAssessment?: GCAssessment): ng.IPromise<number> {
            var listOptions = new Common.ListOptions();
            listOptions.getCount = true;

            var countPromise = this.Get(listOptions, overallAssessment);
            return countPromise;
        }

        // get list of items
        public GetList(skip: number, take: number, orderBy: string[], orderByAsc: boolean, overallAssessment?: GCAssessment): ng.IPromise<GCOverallCompanySummaryExtendedDto[]> {
            var listOptions = new Common.ListOptions();
            listOptions.skip = skip;
            listOptions.take = take;
            listOptions.orderBy = orderBy;
            listOptions.orderAscending = orderByAsc;

            var lstPromise = this.Get(listOptions, overallAssessment);
            return lstPromise;
        }

        //new
        public GetPagedLst(skip: number, take: number, orderBy: string[], orderByAsc: boolean, overallAssessment?: GCAssessment): ng.IPromise<Common.PagedList<GCOverallCompanySummaryExtendedDto>> {
            var ctx = this;

            var getPromise = ctx.GetList(skip, take, orderBy, orderByAsc, overallAssessment);
            var getCountPromise = ctx.GetCount(overallAssessment);

            var getPagedPromise = Common.PagedList.createPagedListFromPromises<GCOverallCompanySummaryExtendedDto>(ctx.$q, getPromise, skip, take, getCountPromise);

            return getPagedPromise;
        }

        // get paged list
        public GetPagedList(pagingOptions: Common.PagingOptions, orderBy: string[], orderByAsc: boolean, overallAssessment?: GCAssessment): ng.IPromise<Common.PagedList<GCOverallCompanySummaryExtendedDto>> {
            var skip = (pagingOptions.pageIndex - 1) * pagingOptions.pageSize;
            var take = pagingOptions.pageSize;

            // get count
            var count: number;
            var countPromise = this.GetCount(overallAssessment);
            countPromise.then(function (countRes) {
                count = countRes;
            });

            // get list
            var lst: GCOverallCompanySummaryExtendedDto[];
            var lstPromise = this.GetList(skip, take, orderBy, orderByAsc, overallAssessment);
            lstPromise.then(function (lstRes) {
                lst = lstRes;
            });

            var pagedListPromise = Common.PagedList.createPagedListFromPromises(this.$q, lstPromise, skip, take, countPromise);
            return pagedListPromise;
        }
    }


    angular.module('Sustainalytics.GCCS').factory('GCOverallCompanySummaryExtendedService', 
        ($q: ng.IQService, $http: ng.IHttpService, notificationFactory: Common.INotificationFactory, staticObjects: Common.IStaticObjects) => {
            var service = new GCOverallCompanySummaryExtendedService($q, $http, notificationFactory, staticObjects);
            return service;
        });
}